23. 练习 — Turtle 方法
练习 - Turtle 方法
你已经学习了使用 turtle 执行不同操作的大量不同方法。我们复习下这些方法,确保你已经记住它们。
QUIZ QUESTION: :
将方法调用与作用相匹配。
ANSWER CHOICES:
作用 |
方法调用 |
---|---|
|
|
|
|
|
|
|
SOLUTION:
作用 |
方法调用 |
---|---|
|
|
|
|
|
|
|
QUIZ QUESTION: :
以下是你刚刚学习过的方法。
同样,将方法调用与作用相匹配。
ANSWER CHOICES:
作用 |
方法调用 |
---|---|
|
|
|
|
|
|
|
SOLUTION:
作用 |
方法调用 |
---|---|
|
|
|
|
|
|
|
现在该你来练习输入方法了。这样有助于你之后需要这些方法时,记住正确的语法。
QUESTION:
输入使
amy
向前移动 100 个像素的方法调用。
SOLUTION:
NOTE: The solutions are expressed in RegEx pattern. Udacity uses these patterns to check the given answer
QUESTION:
输入使
amy
后退 100 个像素的方法调用。
SOLUTION:
NOTE: The solutions are expressed in RegEx pattern. Udacity uses these patterns to check the given answer
QUESTION:
输入使
amy
左转 100 度的方法调用。
SOLUTION:
NOTE: The solutions are expressed in RegEx pattern. Udacity uses these patterns to check the given answer
QUESTION:
输入将
amy
的绘制颜色设为蓝色的方法调用。
SOLUTION:
NOTE: The solutions are expressed in RegEx pattern. Udacity uses these patterns to check the given answer
QUESTION:
有时候,我们希望 turtle 在屏幕上到处移动但是不绘制任何线条。请输入关闭
amy
的笔的方法调用。
SOLUTION:
NOTE: The solutions are expressed in RegEx pattern. Udacity uses these patterns to check the given answer
QUESTION:
输入重新开启
amy
的笔的方法调用,以便在她移动时绘制图形。
SOLUTION:
NOTE: The solutions are expressed in RegEx pattern. Udacity uses these patterns to check the given answer
QUESTION:
输入使
amy
尽可能快速绘制的方法调用。
SOLUTION:
NOTE: The solutions are expressed in RegEx pattern. Udacity uses these patterns to check the given answer
QUESTION:
输入将
amy
的线条粗细更改为 5 个像素的方法调用。
SOLUTION:
NOTE: The solutions are expressed in RegEx pattern. Udacity uses these patterns to check the given answer
现在我们使用这些方法绘制简单的图形:三条彩色线条,中间有空格,如下所示:
在下面的 workspace 中,看看你能否自己画出该图形。我们添加了注释,提醒你应该采取的步骤
(如果遇到问题,请在 workspace 下方查找我们的解决方案。)
Workspace
This section contains either a workspace (it can be a Jupyter Notebook workspace or an online code editor work space, etc.) and it cannot be automatically downloaded to be generated here. Please access the classroom with your account and manually download the workspace to your local machine. Note that for some courses, Udacity upload the workspace files onto https://github.com/udacity , so you may be able to download them there.
Workspace Information:
- Default file path:
- Workspace type: html-live
- Opened files (when workspace is loaded): n/a
( 备注 :如果你无法打开上面的workspace,请去 这里 )
----
解决方案
import turtle
amy = turtle.Turtle()
# Make the width thicker so that the line will be easier to see
# 使线条宽度加粗,以便更容易看到线条
amy.width(5)
# Move back without drawing anything
# 向后移动且不画任何东西
amy.penup()
amy.back(140)
amy.pendown()
# Draw a red line
# 画一条红线(red)
amy.color("red")
amy.forward(50)
# Move forward without drawing anything
# 向前移动且不画任何东西
amy.penup()
amy.forward(50)
amy.pendown()
# Draw an orange line
# 画一条橙色线(orange)
amy.color("orange")
amy.forward(50)
# Move forward without drawing anything
# 向前移动且不画任何东西
amy.penup()
amy.forward(50)
amy.pendown()
# Draw a yellow line
# 画一条黄线(yellow)
amy.color("yellow")
amy.forward(50)